// 引入css样式
require("../../assets/css/common.css");
require("../../assets/font_44fp3ls0tzq/iconfont.css");
require("../../assets/css/reset.css");
require("../../assets/css/normalize.css");
require("../../assets/css/tabbar.less");
require("./index.less");

// 引入swiper轮播图
require("../../libs/swiper-bundle.css");
const Swiper = require("../../libs/swiper-bundle.js");
// 引入模块
const dom = require("../../utils/dom.js");
const axios = require("../../utils/axios.js");

document.addEventListener("DOMContentLoaded", function () {
	// 底部导航栏
	dom.tabBar("index");
	// swiper插件
	var mySwiper = new Swiper(".swiper", {
		loop: true, // 循环模式选项
		autoplay: {
			delay: 1000,
			stopOnLastSlide: false,
			disableOnInteraction: false,
		},
		// 如果需要分页器
		pagination: {
			el: ".swiper-pagination",
		},
	});

	//跳转
	dom.get("#main-five").addEventListener("click", function () {
		location.href = "sports_drill.html";
	});
	dom.get("#main-six").addEventListener("click", function () {
		location.href = "sports_running.html";
	});

	// token:身份认证的令牌（临时），用于标识用户的身份
	// 发请求：获取用户信息，将token作为请求头传入
	axios.get("/api/user/info").then((res) => {
		let data = res.data.data;
		// 排名
		dom.get("#main-one h1").textContent = data.ranking;
		// 徽章badges
		dom.get("#main-four i").textContent = data.badges;
	});

	// 发请求：获取打卡的信息
	let btnIn = dom.get("#main-two button");
	function renderClock() {
		axios.get("/api/user/clockInInfo").then((res) => {
			// 累计打卡天数
			let data = res.data.data;
			dom.get("#main-two b").textContent = data.clockCount;
			// 判断是否打卡
			if (data.isClockIn) {
				//打卡
				btnIn.disabled = true;
				btnIn.style.backgorund = "#999";
				btnIn.textContent = "已打卡";
			}
		});
	}
	renderClock();

	btnIn.addEventListener("click", function () {
		// 发请求：用于打卡
		axios.post("/api/user/clockIn").then((res) => {
			console.log(res);
			if (res.data.errno === 0) {
				renderClock();
			}
		});
	});
});
